package jp.co.cos.ittrick.channel.controller;

import java.util.List;

import jp.co.cos.ittrick.channel.Constants;
import jp.co.cos.ittrick.channel.model.ApplicationModel;
import jp.co.cos.ittrick.channel.model.ChannelModel;
import jp.co.cos.ittrick.channel.model.PageModel;
import jp.co.cos.ittrick.channel.service.ApplicationService;
import jp.co.cos.ittrick.channel.service.ChannelService;
import jp.co.cos.ittrick.channel.service.PageService;
import jp.co.cos.ittrick.channel.util.Utility;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class IndexController extends Controller {

    private ApplicationService applicationService;
    private ChannelService channelService;
    private PageService pageService;

    public IndexController() {
        applicationService = new ApplicationService();
        channelService = new ChannelService();
        pageService = new PageService();
    }

    @Override
    public Navigation run() throws Exception {

        ApplicationModel applicationModel = applicationService.getApplicationModel();
        requestScope("applicationModel", applicationModel);

        String thisPageUrl = "http://" + applicationModel.getApplicationId() + ".appspot.com/";
        requestScope("thisPageUrl", thisPageUrl);

        List<ChannelModel> channelList = channelService.getAll();
        requestScope("channelList", channelList);

        List<PageModel> pageList = null;

        // スマートフォンの場合
        if(Utility.isSmartPhone(request)) {
            pageList = pageService.getPageListOfLimit(0, Constants.SP_INDEX_PAGE_ARTICLE_LIST_LIMIT);
            requestScope("pageList", pageList);

            return forward("/sp/index.jsp");
        }

        // new 記事
        pageList = pageService.getPageListOfLimit(0, Constants.PAGE_ARTICLE_LIST_LIMIT);
        requestScope("pageList", pageList);


        return forward("index.jsp");
    }
}
